package com.kang.common;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

public class R extends HashMap<String, Object> implements Serializable {
	private static final long serialVersionUID = 1L;

	private static final int SUCCESS_CODE = 0;
	private static final int ERROR_CODE = 500;

	public R() {
		put("code", SUCCESS_CODE);
		put("msg", "success");
	}

	public static R error() {
		return error(ERROR_CODE, "Unknown error, please contact the administrator.");
	}

	public static R error(String msg) {
		return error(ERROR_CODE, msg);
	}

	public static R error(int code, String msg) {
		R r = new R();
		r.put("code", code);
		r.put("msg", msg);
		return r;
	}

	public static R ok() {
		return new R();
	}

	public static R ok(String msg) {
		R r = new R();
		r.put("msg", msg);
		return r;
	}

	public static R ok(Map<String, Object> map) {
		R r = new R();
		if (map != null) {
			r.putAll(map);
		}
		return r;
	}

	@Override
	public R put(String key, Object value) {
		super.put(key, value);
		return this;
	}

	public int getCode() {
		return (int) this.getOrDefault("code", ERROR_CODE);
	}

	public String getMsg() {
		return (String) this.getOrDefault("msg", "");
	}
}
